package cn.mavor.day0920;

public class StringCount {
    public static void main(String[] args) {
        String str = "This dog is a good dog.";
        String subStr = "dog";
        int count = countOccurrences(str, subStr);
        System.out.println("The word '" + subStr + "' appears " + count + " times in the sentence.");
    }

    public static int countOccurrences(String str, String subStr) {
        int count = 0;
        int fromIndex = 0;

        while ((fromIndex = str.indexOf(subStr, fromIndex)) != -1) {
            count++;
            fromIndex += subStr.length(); // 移动到找到的子字符串之后，继续查找
        }

        return count;
    }
}